package com.wit.dao.impl;

import java.util.List;
import com.wit.dao.ClientlevelDao;
import com.wit.po.Area;
import com.wit.po.Clientlevel;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import hibernate.HibernateSessionFactory;

public class ClientlevelDaoImpl implements ClientlevelDao {

	
	public boolean addClientlevel(Clientlevel level) {
		
		Session session = null;
		boolean flag = false;
		try {
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			session.save(level);
			tx.commit();
			flag = true;
		
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}

	
	public Clientlevel getClientlevelById(String levelId) {

		if(levelId == null)
			return null;
		Session session = null;
		Clientlevel level = null;
		try {
			String hql = "from Clientlevel level where level.clientlevelId=?";
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
			query.setString(0, levelId);
			level = (Clientlevel)query.uniqueResult();
		
		} catch (Exception e) {

			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return level;
	}

	public static List<Clientlevel> s_GetClientlevelList() {
		
		Session session = null;
		List<Clientlevel> levelList = null;
		try {
			String hql = "from Clientlevel level";
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
			levelList = (List<Clientlevel>)query.list();
		
		} catch (Exception e) {

			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return levelList;
	}
	
	
	public List<Clientlevel> getClientlevelList() {

		return s_GetClientlevelList();
	}

	
	public boolean removeClientlevel(Clientlevel level) {

		if(!isClientlevelRemovable(level.getClientlevelId()))
			return false;
		Session session = null;
		boolean flag = false;
		try {
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			session.delete(level);
			tx.commit();
			flag = true;
		
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}

	
	public boolean updateClientlevel(Clientlevel level) {

		Session session = null;
		boolean flag = false;
		try {
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			session.update(level);
			tx.commit();
			flag = true;
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}
	
	public boolean isClientlevelRemovable(String levelId)
	{
		if(levelId == null)
			return false;
		Session session = null;
		boolean flag = false;
		try {
			String hql = "select count(*) from Client client where client.clientlevel.clientlevelId=?";
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
			query.setString(0, levelId);
			if(((Long)query.uniqueResult()) > 0)
			{
				session.close();
				return false;
			}
			flag = true;
			
		} catch (Exception e) {

			flag = true;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}
}
